<template>
  <a-modal
    title="编辑表单"
    width="60%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="角色名" prop="RoleName">
          <a-input v-model="entity.RoleName" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="角色类型" prop="roletype">
          <a-select show-search v-model="entity.roletype">
            <a-select-option value="服务商用户">服务商用户</a-select-option>
            <a-select-option value="企业用户">企业用户</a-select-option>
            <a-select-option value="税务监管">税务监管</a-select-option>
            <a-select-option value="渠道用户">渠道用户</a-select-option>
            <a-select-option value="平台财务">平台财务</a-select-option>
            <a-select-option value="平台运营">平台运营</a-select-option>
            <a-select-option value="平台风控">平台风控</a-select-option>
            <a-select-option value="平台管理">平台管理</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="上级角色" prop="ParentId">
          <a-tree-select
            v-model="entity.ParentId"
            allowClear
            :treeData="RoleOptionList"
            placeholder="请选择上级角色"
            treeDefaultExpandAll
          ></a-tree-select>

        </a-form-model-item>

        <a-form-model-item label="所属服务商" prop="OrgId">
          <a-select show-search @change="handleChange" v-model="entity.OrgId" allowClear >
            <a-select-option v-for="(item, index) in ServiceList" :key="index" :value="item.Id">
              {{ item.ServiceName }}
              </a-select-option>
          </a-select>
        </a-form-model-item>
        <!-- <a-form-model-item label="数据管辖范围" prop="DataAreaId">
          <a-select v-model="entity.DataAreaId" allowClear >
            <a-select-option v-for="(item, index) in ServiceList" :key="index" :value="item.Id">
              {{ item.ServiceName }}
              </a-select-option>
          </a-select>
        </a-form-model-item> -->
        <a-form-model-item label="权限" prop="AppSecret">
          <a-tree
            showLine
            v-if="actionsTreeData && actionsTreeData.length"
            checkable
            @check="onCheck"
            :autoExpandParent="false"
            v-model="checkedKeys"
            :treeData="actionsTreeData"
            :defaultExpandAll="false"
            :checkStrictly="true"
          />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import TreeHelper from '@/utils/helper/TreeHelper'

export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      confirmLoading: false,
      entity: {},
      actionsTreeData: [],
      allActionList: [],
      checkedKeys: { checked: [] },
      RoleOptionList:[],
      ServiceList:[],
      rules: {
        RoleName: [{ required: true, message: '必填' }]
      }
    }
  },
  methods: {
   async init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.$http.post("/Base_Manage/Home/GetService_Select").then((res)=>{
        this.ServiceList = res.Data
      })
      await this.$http.post('/Base_Manage/Base_Role/GetTreeDataList', {}).then(resJson => {
        if (resJson.Success) {
          this.RoleOptionList = resJson.Data
        }
      })
      await this.$http.post('/Base_Manage/Base_Action/GetActionTreeList', {}).then(resJson => {
        if (resJson.Success) {
          this.actionsTreeData = resJson.Data
        }
      })
      await this.$http.post('/Base_Manage/Base_Action/GetAllActionList', {}).then(resJson => {
        if (resJson.Success) {
          this.allActionList = resJson.Data
        }
      })
    },
    openForm(id) {
      this.init()

      if (id) {
        this.$http.post('/Base_Manage/Base_Role/GetTheData', { id: id }).then(resJson => {
          this.entity = resJson.Data

          this.checkedKeys = { checked: this.entity['Actions'] }
        })
      }
    },
    onCheck(checkedKeys, e) {
      // console.log('勾选')
      // console.log(checkedKeys)
      // console.log(this.checkedKeys)
      //勾选事件,勾选节点时同时勾选所有父节点和子节点
      var value = e.node.value
      var newChecked = []
      if (e.checked) {
        var parentIds = TreeHelper.getParentIds(value, this.allActionList)
        var children = TreeHelper.getChildrenIds(value, this.allActionList)
        var addNodes = parentIds.concat(children).filter(item => !this.checkedKeys.checked.includes(item))
        newChecked = this.checkedKeys.checked.concat(addNodes)
      } else {
        //取消勾选事件,取消勾选所有子节点
        var children = TreeHelper.getChildrenIds(value, this.allActionList)
        children.push(value)
        newChecked = this.checkedKeys.checked.filter(item => !children.includes(item))
      }

      this.checkedKeys = { checked: newChecked }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.confirmLoading = true
        this.entity['Actions'] = this.checkedKeys.checked
        this.$http.post('/Base_Manage/Base_Role/SaveData', this.entity).then(resJson => {
          this.confirmLoading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.afterSubmit()
            this.visible = false
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
    handleChange(value){
      let obj = {};
         obj = this.ServiceList.find((item)=>{ //这里的Data就是上面遍历的数据源
           return item.Id === value; //筛选出匹配数据
         });
      this.entity.OrgName = obj.ServiceName
      console.log(this.entity)
    }
  }
}
</script>
